//ref：https://blog.csdn.net/qq_41673920/article/details/81390972 

#include <string.h>

char *strrpc(char *str,char *oldstr,char *newstr);
/* 功  能：将str字符串中的oldstr字符串替换为newstr字符串
 * 参  数：str：操作目标 oldstr：被替换者 newstr：替换者
 * 返回值：返回替换之后的字符串
 */
char *strrpc(char *str,char *oldstr,char *newstr){
    if(!str || !oldstr || !newstr) return NULL;

    char bstr[strlen(str)];//转换缓冲区

    memset(bstr,0,sizeof(bstr));
 
    for(int i = 0;i < strlen(str);i++){
        if(!strncmp(str+i,oldstr,strlen(oldstr))){//查找目标字符串
            strcat(bstr,newstr);
            i += strlen(oldstr) - 1;
        }else{
        	strncat(bstr,str + i,1);//保存一字节进缓冲区
	    }
    }
 
    strcpy(str,bstr);
    return str;
}
#if 0
    // for Demo
#include <stdio.h>

int main(void)
{
    char test[64] = {"hello, world"};
    char * p_ret = NULL;

    printf("Test for strcpt:\n");
    printf("str:\n\t%s\n", test);

    p_ret = strrpc(test,"ll", "xixi");
    if(p_ret == NULL)
    {
        printf("Err\n");
        return -1;
    }
    printf("str:\n\t%s\n", test);

    return 0;
}
#endif
